package com.jams.goodox.activity.mine.interaction;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hangzhou.jin.customview.JRecyclerView;
import com.hangzhou.jin.customview.RecycleViewDivider;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.adapter.RecommendAdapter;
import com.jams.goodox.entity.resp.MyRecommendResp;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * 我的推荐模块
 * Created by jams_zhen on 2016/12/27.
 */

public class RecommendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private JRecyclerView jRecyclerView;
    private RecommendAdapter mAdapter;
    private List<MyRecommendResp.DataBean.ResultListBean> mList;

    private static final int PAGE_SIZE = 20;
    private int curPage = 1;

    private MyRecommendResp recommendResp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recommend_list_fragment, container, false);
        jRecyclerView = (JRecyclerView) view.findViewById(R.id.recycler_view);
        init();
        return view;
    }

    /**
     * 初始化
     */
    private void init() {

        jRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RecommendAdapter(mList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, MyRecommendResp.DataBean.ResultListBean result) {
                final int status = result.orderStatus;
                //订单状态
                String orderStatus ="";
                //订单状态的显示颜色
                int drawable  = R.drawable.circle_btn_gray;
                switch (status) {
                    case 0:
                        drawable = R.drawable.circle_btn_green;
                        orderStatus = "已推荐";
                        break;
                    case 1:
                        drawable = R.drawable.circle_btn_blue;
                        orderStatus = "已支付";
                        break;
                    case 2:
                        drawable = R.drawable.circle_btn_red;
                        orderStatus = "不匹配";
                        break;
                    default:
                        break;
                }
                baseViewHolder.setText(R.id.tx_name, result.userName)
                        .setText(R.id.tx_state,orderStatus)
                        .setText(R.id.tx_pay_money,result.reward+"元")
                        .setText(R.id.tx_company,result.enterpriseName)
                        .setImageDrawable(R.id.iv_state,getResources().getDrawable(drawable));
            }
        };
        jRecyclerView.setAdapter(mAdapter);
        jRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 1,getResources().getColor(R.color.base_background)));
        mAdapter.openLoadMore(PAGE_SIZE, true);

        // 自定义上拉View
        mAdapter.setLoadingView(getLoadMoreBootView(mContext));

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData(false);
            }
        });

        jRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 刷新代码
                loadData(true);
            }
        });
        jRecyclerView.getParentView().setBackgroundResource(R.color.base_background);
        jRecyclerView.loading();
        loadData(true);

    }

    /**
     * 获取数据
     *
     * @param isRefresh
     */
    private void loadData(final boolean isRefresh) {
      /*  HashMap<String, String> params = new HashMap<>();
        params.put("curPage", (isRefresh ? 1 : curPage + 1) + "");
        params.put("pageSize", PAGE_SIZE + "");*/
        enqueue(createCallService(MineService.class).MyRecommendInteraction(isRefresh ? 1 : (curPage + 1), PAGE_SIZE), new RetrofitCallbackListener<MyRecommendResp>() {
            @Override
            public void onSuccess(Call<MyRecommendResp> call, MyRecommendResp result) {
                jRecyclerView.loadComplete();
                jRecyclerView.setRefreshing(false);
                recommendResp = result;
                if (recommendResp == null || recommendResp.data == null) {
                    showNetErrorToast();
                    return;
                }
                if (!recommendResp.getStatus().equals(ProtocolManagers.SUCCESS)) {
                    ToastUtil.show(recommendResp.getMsg());
                    return;
                }
                mList = recommendResp.data.resultList;
                if (recommendResp.data.pageInfo != null)
                    curPage = recommendResp.data.pageInfo.curPage;
                if (mList == null) return;
                if (isRefresh) {
                    mAdapter.setNewData(mList);
                    mAdapter.notifyDataChangedAfterLoadMore(mList.size() == PAGE_SIZE);
                    if (mList.size() == 0) {
                        jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                        //   return;
                    }
                } else {
                    mAdapter.notifyDataChangedAfterLoadMore(mList, mList.size() == PAGE_SIZE);
                }
            }

            @Override
            public void onError(String msg) {
                showNetErrorToast();
                if (!CollectionUtils.hasData(mList)) {
                    jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                } else {
                    jRecyclerView.loadComplete();
                }

                jRecyclerView.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        loadData(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
